package com.zhouxiaoxuan13.promotionservice.controller;

import com.zhouxiaoxuan13.promotionservice.entity.FreshProduct;
import com.zhouxiaoxuan13.promotionservice.service.FreshProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/fresh-products")
@CrossOrigin(originPatterns = "*")
public class FreshProductController {

    @Autowired
    private FreshProductService freshProductService;

    // GET: 获取所有生鲜商品
    @GetMapping
    public ResponseEntity<List<FreshProduct>> getAllFreshProducts() {
        List<FreshProduct> products = freshProductService.getAllFreshProducts();
        return ResponseEntity.ok(products);
    }

    // GET: 根据ID获取单个生鲜商品
    @GetMapping("/{id}")
    public ResponseEntity<FreshProduct> getFreshProductById(@PathVariable("id") Long id) {
        return freshProductService.getFreshProductById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // GET: 根据名称搜索生鲜商品
    @GetMapping("/search")
    public ResponseEntity<List<FreshProduct>> searchFreshProducts(@RequestParam("name") String name) {
        List<FreshProduct> products = freshProductService.searchFreshProductsByName(name);
        return ResponseEntity.ok(products);
    }

    // GET: 根据种类获取生鲜商品
    @GetMapping("/category/{category}")
    public ResponseEntity<List<FreshProduct>> getFreshProductsByCategory(@PathVariable("category") String category) {
        List<FreshProduct> products = freshProductService.getFreshProductsByCategory(category);
        return ResponseEntity.ok(products);
    }

    // GET: 获取所有促销商品
    @GetMapping("/promotions")
    public ResponseEntity<List<FreshProduct>> getPromotionProducts() {
        List<FreshProduct> products = freshProductService.getPromotionProducts();
        return ResponseEntity.ok(products);
    }

    // GET: 获取库存预警商品
    @GetMapping("/low-stock")
    public ResponseEntity<List<FreshProduct>> getLowStockProducts(@RequestParam(value = "threshold", defaultValue = "10") Integer threshold) {
        List<FreshProduct> products = freshProductService.getLowStockProducts(threshold);
        return ResponseEntity.ok(products);
    }

    // POST: 创建新的生鲜商品
    @PostMapping
    public ResponseEntity<FreshProduct> createFreshProduct(@RequestBody FreshProduct freshProduct) {
        FreshProduct createdProduct = freshProductService.createFreshProduct(freshProduct);
        return new ResponseEntity<>(createdProduct, HttpStatus.CREATED);
    }

    // PUT: 更新生鲜商品
    @PutMapping("/{id}")
    public ResponseEntity<FreshProduct> updateFreshProduct(@PathVariable("id") Long id, @RequestBody FreshProduct freshProductDetails) {
        return freshProductService.updateFreshProduct(id, freshProductDetails)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // DELETE: 删除生鲜商品
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFreshProduct(@PathVariable("id") Long id) {
        if (freshProductService.deleteFreshProduct(id)) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
